{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义变量\n",
    "a = tf.Variable(0, name=\"a\")\n",
    "b = tf.Variable(0, name=\"b\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义保存模型\n",
    "saver = tf.train.Saver()\n",
    "save_dir = \"../save_path/test_model/\"\n",
    "\n",
    "# 定义模型序号\n",
    "step = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "v1 = 1\n",
      "v2 = 2\n"
     ]
    }
   ],
   "source": [
    "with tf.Session() as sess:\n",
    "    # 恢复保存模型\n",
    "    # 如果有检查点文件, 读取最新的检查点文件，恢复各种变量值\n",
    "    ckpt_dir = tf.train.latest_checkpoint(save_dir)\n",
    "    if ckpt_dir != None:\n",
    "        saver.restore(sess, ckpt_dir)\n",
    "    else:\n",
    "        # 变量初始化\n",
    "        sess.run(tf.initialize_all_variables())\n",
    "    # 或者直接读取\n",
    "    # saver.restore(sess, save_dir + \"model-{}\".format(step))\n",
    "    print(\"v1 =\", a.eval())\n",
    "    print(\"v2 =\", b.eval())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
